﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GONES.IBLL;
using GONES.IDAL;
using GONES.Model;
using log4net;
using System.Reflection;
using GONES.Tools.Common;
using Newtonsoft.Json;

namespace GONES.BLL
{
    public class S_RolePermissionManager : IS_RolePermissionManager
    {
        public IS_RolePermissionDao s_RolePermissionDao;

        protected static readonly ILog Log =
            LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public IS_LogDao S_LogDao;
        private readonly IUserInfo _userInfo;
        public S_LogManager S_LogManager;
        public string ErrMsg { get; set; }

        public S_RolePermissionManager(IUserInfo userinfo)
        {
            _userInfo = userinfo;
        }

        public void Add(S_RolePermission rolepermission)
        {
            if (IsError(rolepermission))
                return;
            rolepermission.CreateTime = System.DateTime.Now;
            s_RolePermissionDao.Save(rolepermission);
            SaveLog("角色权限管理", "角色权限添加", "系统管理", rolepermission);
        }

        public void Edit(S_RolePermission rolepermission)
        {
            if (IsError(rolepermission))
                return;
            rolepermission.ModifyTime = System.DateTime.Now;
            s_RolePermissionDao.Update(rolepermission);
            SaveLog("角色权限管理", "角色权限修改", "系统管理", rolepermission);
        }

        public void Delete(string id)
        {
            S_RolePermission rolepermission = s_RolePermissionDao.Get(id);
            s_RolePermissionDao.Delete(rolepermission);
            SaveLog("角色权限管理", "角色权限删除", "系统管理", rolepermission);
        }

        public IList<S_RolePermission> GetRolePermissionList(string roleid)
        {
            return s_RolePermissionDao.GetHqlList(string.Format("select e from S_RolePermission e where RoleId='{0}'", roleid));
        }

        public IList<S_RolePermission> GetRolePermissionList(string roleid,string navpermisionId)
        {
            return s_RolePermissionDao.GetHqlList(string.Format("select e from S_RolePermission e where RoleId='{0}' and NavPermissionid='{1}'", roleid,navpermisionId));
        }


        private void SaveLog(string module, string operate, string logtype, S_RolePermission rolepermission)
        {
            S_LogManager = new S_LogManager(_userInfo);
            string detail = string.Format("{0}:角色ID是{1}操作权限ID是{2}", operate, rolepermission.RoleId, rolepermission.NavPermissionid);
            SaveLog(module, operate, logtype, detail);
            if (Log.IsInfoEnabled)
                Log.Info(string.Format("{0}：{1}", operate, JsonConvert.SerializeObject(rolepermission)));
        }
 
        protected bool IsError(S_RolePermission rolepermission)
        {
            if ((string.IsNullOrEmpty(rolepermission.RoleId)))
            {
                ErrMsg = "角色ID不能为空!";
            }
            if (string.IsNullOrEmpty(rolepermission.NavPermissionid))
            {
                ErrMsg = "操作权限ID不能为空!";
            }
            if (!string.IsNullOrEmpty(ErrMsg))
                return true;
            return false;
        }

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="operate">操作名称</param>
        /// <param name="logtype">日志类型（系统管理、基础资料等）</param>
        /// <param name="detail">详细日志</param>
        protected void SaveLog(string module, string operate, string logtype, string detail)
        {
            try
            {
                S_LogDao.Save(new S_Log
                {
                    ID = Rnd.Guid.ToString(),
                    Module = module,
                    Operate = operate,
                    Detail = detail,
                    LogType = logtype,
                    OpDate = DateTime.Now,
                    CreateUser = _userInfo.CLoginInfo.UserName,
                    CreateTime = DateTime.Now,
                    ModifyUser = _userInfo.CLoginInfo.UserName,
                    ModifyTime = DateTime.Now,
                    Ip = Tools.Common.Tools.GetIP()
                });
            }
            catch
            {
            }
        }
    }
}
